let express = require("express");
// 载入snmp模块
let snmp = require("net-snmp");
const { json } = require("sequelize");
//监控设备的ip
let session = snmp.createSession("192.168.8.142", "public");
let maxRepetitions = 20;
//snmp 协议 oid查询对节点的所有状态
let oid = "1.3.6.1.4.1.46861";
// 路由
let reouter = express.Router();
const bodyParser = require("body-parser");

// 加载模型数据库模块
let { snmp_test } = require("../config/dbconfig");

const getSnmpData = () => {
  return new Promise((resolve, reject) => {
    const list = [];
    // 错误方法
    async function doneCb(error) {
      if (error) {
        // 错误信息
        reject(error.toString());
      } else {
        resolve(list);
      }
    }
    // 接收数据
    function feedCb(varbinds) {
      // console.log(varbinds);
      for (var i = 0; i < varbinds.length; i++) {
        if (snmp.isVarbindError(varbinds[i])) {
          // console.error(34,snmp.varbindError(varbinds[i]));
          list.push(snmp.varbindError(varbinds[i]));
          // console.log (varbinds[i].oid + "|" + varbinds[i].value);
        } else {
          console.log(varbinds[i].oid + "|" + varbinds[i].value);
          // 解决字符串转码
          varbinds[i].value = String(varbinds[i].value);
          list.push(varbinds[i]);
        }
      }
    }
    // 调用执行snnmp协议获取状态
    session.walk(oid, maxRepetitions, feedCb, doneCb);
  });
};
//单条或多条查询
const getSnmpOid = (snmpOid) => {
  return new Promise((resolve, reject) => {
    let list = [];
    session.get(snmpOid, function (error, varbinds) {
      if (error) {
        console.error(error.toString());
        reject(error.toString());
      } else {
        for (var i = 0; i < varbinds.length; i++) {
          // for version 1 we can assume all OIDs were successful
          // console.log(varbinds[i].oid + "|" + varbinds[i].value);

          // for version 2c we must check each OID for an error condition
          if (snmp.isVarbindError(varbinds[i]))
            console.error(snmp.varbindError(varbinds[i]));
          else {
            // console.log(varbinds[i].oid + "|" + varbinds[i].value);
            // 解决字符串转码
            varbinds[i].value = String(varbinds[i].value);
            list.push(varbinds[i]);
            resolve(list);
          }
        }
      }
    });
  });
};

reouter.post("/snmp", async (req, res, next) => {
  try {
    const rs = await getSnmpData();
    //   成功
    res.send({
      meta: {
        msg: "查询成功",
        staus: 200,
      },
      data: rs,
    });
  } catch (error) {
    res.send({
      meta: {
        msg: "查询失败，没有连接",
        staus: 201,
      },
      data: error,
    });
  }
});
//snmp oid查询
reouter.post("/snmpOid", bodyParser.json(), async (req, res, next) => {
  let { oids } = req.body;
  // let oids = JSON.parse(req.body.oid)
  // 数组形式传入
  try {
    const rs = await getSnmpOid(oids);
    res.send({
      meta: {
        msg: "查询成功",
        staus: 200,
      },
      data: rs,
    });
  } catch (error) {
    res.send({
      meta: {
        msg: "查询失败，没有连接",
        staus: 201,
      },
      data: error,
    });
  }
});

module.exports = reouter;
